package com.flying.um.adapter.spring;

import com.flying.um.domain.service.UserService;
import com.flying.um.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

public class SpringUserDetailsService implements UserDetailsService {
    @Autowired
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.findByName(username);
        System.out.println(user.getRoles());
        if (user == null) {
            throw new UsernameNotFoundException("Username" + username + "not foud");
        }
        return new SecurityUser(user);
    }
}
